package jgl.vajra.driver.part.vehicle.pop;

import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import com.orhanobut.logger.Logger;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import jgl.vajra.driver.R;
import jgl.vajra.driver.bean.RewardBean;
import jgl.vajra.driver.databinding.PopRewardBinding;
import jgl.vajra.driver.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.driver.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.driver.widget.utils.ToastUtils;
import jgl.vajra.driver.widget.utils.UIUtils;

public class RewardPop implements BaseBindingItemPresenter<RewardBean> {
    private WeakReference<FragmentActivity> activityWeakReference;
    private PopupWindow menuPop;
    private View mPopRootView;
    private PopRewardBinding mBinding;
    private List<RewardBean> listReward = new ArrayList<>();
    private SingleTypeBindingAdapter adapter;
    private RewardBean itemData;

    public RewardPop(FragmentActivity activity) {
        activityWeakReference = new WeakReference<>(activity);
    }

    private void initPopupWindow() {
        mPopRootView = View.inflate(UIUtils.getContext(), R.layout.pop_reward, null);
        mBinding = DataBindingUtil.bind(mPopRootView);
        if (mBinding != null)
            mBinding.setView(this);

        menuPop = new PopupWindow(mPopRootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        menuPop.setOutsideTouchable(false);
        menuPop.setBackgroundDrawable(new BitmapDrawable());
        menuPop.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
        menuPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                Logger.d("------onDismiss");
                //popupwindow消失的时候恢复成原来的透明度
                // backgroundAlpha(R.color.gray_f8);
                backgroundAlpha(1f);

            }
        });
        listReward.addAll(RewardBean.getListData());

        // 不可嵌套滑动
        mBinding.recyclerView.setNestedScrollingEnabled(false);
        // 取消head 的 recycleView的焦点
        mBinding.recyclerView.setFocusableInTouchMode(false);
        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new GridLayoutManager(UIUtils.getContext(), 4));
        adapter = new SingleTypeBindingAdapter(UIUtils.getContext(), listReward, R.layout.item_pop_reward);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

    }


    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        if (activityWeakReference.get() == null) return;
        if (activityWeakReference != null) {
            WindowManager.LayoutParams lp = activityWeakReference.get().getWindow().getAttributes();
            lp.alpha = bgAlpha; //0.0-1.0
            activityWeakReference.get().getWindow().setAttributes(lp);
        }
    }

    public void showPopupWindow(View v) {
        if (menuPop == null)
            initPopupWindow();
        int[] location = new int[2];
        v.getLocationOnScreen(location);  //获取控件的位置坐标
        //获取自身的长宽高
        mPopRootView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        //backgroundAlpha(R.color.titleBackgroundColor);
        backgroundAlpha(0.8f);

        menuPop.showAtLocation(v, Gravity.BOTTOM, 0, 0);  //显示指定控件的上方

    }

    public boolean isShowing() {
        return menuPop != null && menuPop.isShowing();
    }

    public void dismiss() {
        if (isShowing()) {
            menuPop.dismiss();
        }
    }

    //确定
    public void onDetermine(){
        String amountMoney;
        if(itemData.isCustom){
            amountMoney= mBinding.etAmountMoney.getText().toString().trim();
        }else {
            amountMoney=itemData.name;
        }
        ToastUtils.showShort("金额"+amountMoney);
    }

    @Override
    public void onItemClick(int position, RewardBean itemData) {
        this.itemData = itemData;
        if (itemData.isClick)
            return;
        for (RewardBean item : listReward) {
            item.isClick = false;
        }
        listReward.get(position).isClick = true;
        adapter.refresh();

        mBinding.etAmountMoney.setText("");
        mBinding.etAmountMoney.setVisibility(itemData.isCustom?View.VISIBLE:View.GONE);
    }
}
